#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include "File_System_Operations.h"
#include "header.h"

struct Main_header MAN;
struct file_descriptor FDESC;
struct Block BLOCK;
FILE *vfptr;
struct file_descriptor *fdesc[100];


int create_vfs (char vfs_label[],unsigned int size) {
	
	int i;
 	vfptr=fopen(vfs_label,"wb+");// opening a file in binary mode with vfs_label name
		 if(vfptr==NULL){
			printf("file not created"); 
			return;
		}
	MAN.Max_fd=size/1024;
	int h=(sizeof(MAN)+(sizeof(FDESC)*MAN.Max_fd));
	printf(" Size of man:%d\n",sizeof(MAN));
	printf(" Size of fdesc:%d\n",sizeof(FDESC));
	fwrite(&MAN,sizeof(MAN),1,vfptr);
	for( i=0;i<(MAN.Max_fd);i++){
	  fwrite(&FDESC,sizeof(FDESC),1,vfptr);
	  fdesc[i]=&vfptr;	
	}
	printf("Cursor1: %ld",ftell(vfptr));
	MAN.Max_fd=(size-h)/1024;
	MAN.fd_in_use=0;
	 fseek ( vfptr, h  , SEEK_SET ) ; 
	
	// putting the file to the begining of the file
	 /*long int h=ftell(vfptr);
	 long int diff = size - h ;*/
 	for(i=0;i<MAN.Max_fd;i++){
		 fwrite(&BLOCK, sizeof(BLOCK),1,vfptr);// dividing the file into fixed blocks in to 1024 bytes
		//long int z=ftell(vfptr); 
		//printf(" Asssign: %ld\n",z);
		}
	 //fseek(vfptr,h,SEEK_SET);
		
		//fseek(vfptr,0,SEEK_SET);
	
	/* for(i=1;i<MAN.Max_fd;i++){
		fdesc[i]=(fdesc[i-1]+1024);
		//printf(" ptr:%x\n",fdesc[i]);
		}*/
	//printf("%s",(*fdesc[0]));
	 
	//printf("%d",sizeof(MAN));
	printf("Cursor:%ld",ftell(vfptr));
	//printf("%d",sizeof(FDESC));
	//printf("%s",*(fdesc[0]));
	fseek(vfptr,h,SEEK_SET);
	 
	printf("cursur:%ld",ftell(vfptr));
	 printf("\nFile System Successfuly created with attributes:");
	 printf("\nVFS Name : %s",MAN.vf_File_Label);
	 printf("\nMaximum File Desscriptors :%d",MAN.Max_fd);
	 printf("\nFile Descriptors in use :%d",MAN.fd_in_use);
	 printf("\nSuccessfully created blocks of size : %d\n",sizeof(BLOCK));// displaying the attributes of the VFS_File_System
	 //fclose(vfptr);	
	 return 1;
}

int mount_vfs (char* vfs_label) {
	printf("\nSuccessfully mounted.\n");
	return 1;

}

int unmount_vfs (char* vfs_label) {
        //flcose(vfptr);
	return 1;
}


